package svc

import (
	"mall/service/attachment/rpc/attachmentclient"
	"mall/service/backend/rpc/backendclient"
	"mall/service/blog/rpc/blogclient"
	"mall/service/forum/rpc/forumclient"
	"mall/service/layout/rpc/layout"
	"mall/service/setting/rpc/settingclient"
	"mall/service/user/rpc/userclient"
	"mall/service/wpgql/api/internal/config"
	"mall/service/wpgql/api/internal/graph/auth"
	"mall/service/wpgql/api/internal/graph/storage"

	"github.com/zeromicro/go-zero/zrpc"
	"github.com/zeromicro/go-zero/rest"
)

type ServiceContext struct {
	Config config.Config

	Storage        storage.Storage
	AuthMiddleware rest.Middleware

	AttachmentRpc attachmentclient.Attachment
	BackendRpc    backendclient.Backend
	BlogRpc       blogclient.Blog
	ForumRpc      forumclient.Forum
	LayoutRpc     layout.Layout
	SettingRpc    settingclient.Setting
	UserRpc       userclient.User
}

func NewServiceContext(c config.Config) *ServiceContext {
	// instantiate the DB client
	db := storage.NewMemoryStorage()
	return &ServiceContext{
		Config:         c,
		Storage:        db,
		AuthMiddleware: auth.NewAuthMiddleware(db).Handle,
		AttachmentRpc:  attachmentclient.NewAttachment(zrpc.MustNewClient(c.AttachmentRpc)),
		BackendRpc:     backendclient.NewBackend(zrpc.MustNewClient(c.BackendRpc)),
		BlogRpc:        blogclient.NewBlog(zrpc.MustNewClient(c.BlogRpc)),
		ForumRpc:       forumclient.NewForum(zrpc.MustNewClient(c.ForumRpc)),
		LayoutRpc:      layout.NewLayout(zrpc.MustNewClient(c.LayoutRpc)),
		SettingRpc:     settingclient.NewSetting(zrpc.MustNewClient(c.SettingRpc)),
		UserRpc:        userclient.NewUser(zrpc.MustNewClient(c.UserRpc)),
	}
}
